package com.wsc.wisdomchina.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.activitity.ExaminationActivity;
import com.wsc.wisdomchina.javaBean.QuestionBean;

import java.util.List;

/**
 * Created by admin on 2017/9/27.
 */

public class ExamFragment extends Fragment {
    private QuestionBean mBean;
    private View mView;
    private RadioGroup mGroup;
    private TextView mTitle;

    public void setData(QuestionBean mBean) {
        this.mBean = mBean;

    }


    private void initViewData() {
        if(mBean == null){
            Toast.makeText(getActivity(),"无法获取试题信息",Toast.LENGTH_SHORT).show();
          return;
        }
        mTitle.setText(mBean.getTopic());
        List<QuestionBean.Option> list = mBean.getAnswers();
        for(QuestionBean.Option qbo:list){

            RadioButton radioButton = new RadioButton(getActivity());
            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
            //设置RadioButton边距 (int left, int top, int right, int bottom)
            lp.setMargins(15,0,0,0);
            //设置RadioButton背景
            //radioButton.setBackgroundResource(R.drawable.xx);
            //设置RadioButton的样式
            //设置文字距离四周的距离
            radioButton.setPadding(30, 0, 0, 0);
            //设置文字
            radioButton.setText(qbo.getAnswer());
            radioButton.setTag(mBean.getCorrectAnswer());
            //设置radioButton的点击事件
            final  String tag = qbo.getNum();
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(getActivity(), "this is radioButton  " , Toast.LENGTH_SHORT).show();
                    ExaminationActivity activity = (ExaminationActivity)getActivity();
                    activity.updateAnswer(mBean.getId(),tag);
                    activity.nextQuestion();
                }
            });
            //将radioButton添加到radioGroup中
            mGroup.addView(radioButton);
        }
/*        mGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup,  int i) {
                Toast.makeText(getActivity(), "posotion  " + i , Toast.LENGTH_SHORT).show();
            }
        });*/
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.exam_fragment_layout, null);
        mTitle = (TextView) mView.findViewById(R.id.tv_topic);
        mGroup = (RadioGroup) mView.findViewById(R.id.rg_content);
        initViewData();
        return mView;
    }
}
